﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GenerationCode
{
    public partial class frmSetCacheDataTable : Form
    {
        #region Fields
        public List<Utils.Table> lstTable = null;     
      
        #endregion

        #region Init,Load       
        public frmSetCacheDataTable(List<Utils.Table> tableList)
        {
            InitializeComponent();
            this.lstTable = tableList;

        }
        private void frmSetCacheDataTable_Load(object sender, EventArgs e)
        {
            BindData();
        }
       
        #endregion      

        #region Method,function
        private void BindData()
        {
            try
            {
                for (int i = 0; i < lstTable.Count; i++)
                {
                   // if (!lstTable[i].Name.Contains(Utils.Utility.MultilanguageTableName))
                    //{
                        lstTable[i].IsMultilanguage = CheckMultilanguage(lstTable[i].Name);
                        gvTable.Rows.Add(lstTable[i].Name,true, false,lstTable[i].IsMultilanguage);
                    //}
                }
            }
            catch
            {
                
            }
        }

        private bool CheckMultilanguage(string tableName)
        {
            for (int i = 0; i < lstTable.Count; i++)
            {
                if (lstTable[i].Name.Trim() == tableName + Utils.Utility.MultilanguageTableName)
                    return true;
            }
            return false;
        }
        #endregion      
        #region Event
        private void chkCheckAll_CheckedChanged(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.CheckBox)(sender)).Checked)
                SetCheckValue(true,"IsCacheData");
            else
                SetCheckValue(false,"IsCacheData");
        }
        private void SetCheckValue(bool value,string colum)
        {
            for (int i = 0; i < gvTable.Rows.Count; i++)
            {
                gvTable.Rows[i].Cells[colum].Value = value;
            }
        }
        #endregion

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            SetCacheDataTable();            
            this.Close();
        }
        private void SetCacheDataTable()
        {
            for (int i = 0; i < gvTable.Rows.Count; i++)
            {
                string tableName=gvTable.Rows[i].Cells["TableName"].Value.ToString();
                bool isSelect = (Boolean)gvTable.Rows[i].Cells["Select"].Value;
               
                    bool IsCacheData = (Boolean)gvTable.Rows[i].Cells["IsCacheData"].Value;
                    bool isMultiLanguage = (Boolean)gvTable.Rows[i].Cells["MultiLanguage"].Value;
                    SetCacheDataTable(tableName,isSelect,IsCacheData, isMultiLanguage);
               
            }
        }
        private void SetCacheDataTable(string tableName, bool isSelect, bool isCache, bool isMultilanguage)
        {
            for (int i = 0; i < lstTable.Count; i++)
            {
                if (lstTable[i].Name.Trim() == tableName.Trim())
                {
                    lstTable[i].IsSelect = isCache;
                    lstTable[i].IsCacheData = isCache;
                    lstTable[i].IsMultilanguage = isMultilanguage;
                }
            }
        }

        private void chkMultilanguage_CheckedChanged(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.CheckBox)(sender)).Checked)
                SetCheckValue(true,"MultiLanguage");
            else
                SetCheckValue(false,"MultiLanguage");
        }

        private void chkSelect_CheckedChanged(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.CheckBox)(sender)).Checked)
                SetCheckValue(true, "Select");
            else
                SetCheckValue(false, "Select");
        }

    }
}
